package com.koron.order.linedefect.base.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.query.PageQuery;
import com.koron.order.common.tools.CommonCodeTools;
import com.koron.order.common.tools.OrderCodeEnum;
import com.koron.order.linedefect.base.bean.po.DefectBean;
import com.koron.order.linedefect.base.bean.po.DefectChange;
import com.koron.order.linedefect.base.bean.query.DefectChangeQuery;
import com.koron.order.linedefect.base.bean.vo.DefectChangeVO;
import com.koron.order.linedefect.base.mapper.DefectChangeMapper;
import com.koron.order.linedefect.base.service.DefectChangeService;
import com.koron.order.linedefect.base.service.DefectService;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

@Service
@Slf4j
public class DefectChangeServiceImpl implements DefectChangeService {


    @Autowired
    private DefectService defectService;


    @TaskAnnotation("saveOrUpdate")
    @Override
    public Response saveOrUpdate(SessionFactory factory, DefectChange defectChange) {
        DefectChangeMapper mapper = factory.getMapper(DefectChangeMapper.class);

        String id = defectChange.getId();
        if (StrUtil.isEmpty(id)) {
            DefectBean defectBean = defectService.queryById(factory, defectChange.getLineDefectId());
            defectChange.setCode(CommonCodeTools.getNormalOrderCode(defectBean.getCode(), OrderCodeEnum.BG, 2));
        }

        boolean result = mapper.save(defectChange);
        return Response.ok(defectChange);
    }

    @TaskAnnotation("delete")
    @Override
    public Response delete(SessionFactory factory, String id) {
        Assert.notNull(id, "id不能为空！");
        DefectChangeMapper mapper = factory.getMapper(DefectChangeMapper.class);
        boolean result = mapper.delete(id);
        return Response.ok(result);
    }

    @TaskAnnotation("page")
    @Override
    public Response page(SessionFactory factory, PageQuery pageQuery, DefectChangeQuery defectChangeQuery) {
        pageQuery.getOrders().forEach(x-> x.setPrefix("wldc"));
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy());
        DefectChangeMapper mapper = factory.getMapper(DefectChangeMapper.class);
        List<DefectChangeVO> list = mapper.list(defectChangeQuery);
        return Response.ok(new PageInfo<>(list));
    }

    @TaskAnnotation("getById")
    @Override
    public Response getById(SessionFactory factory, String id) {
        Assert.notNull(id,"id不能为空！");
        DefectChangeMapper mapper = factory.getMapper(DefectChangeMapper.class);
        DefectChangeVO defectChangeVO = mapper.getById(id);
        return Response.ok(defectChangeVO);
    }

    @TaskAnnotation("update")
    @Override
    public Response update(SessionFactory factory, String id, Integer status) {
        Assert.notNull(id, "id不能为空！");
        Assert.notNull(status, "status不能为空！");
        DefectChangeMapper mapper = factory.getMapper(DefectChangeMapper.class);
        return Response.ok(mapper.update(id, status));
    }
}
